//
//  SetViewController.m
//  LT
//
//  Created by dllo on 16/5/25.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "SetViewController.h"
#import "UMSocial.h"
#import "loginViewController.h"


#define WITDH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height

static NSString *const setCell = @"setCell";

@interface SetViewController ()<UITableViewDataSource,UITableViewDelegate,UMSocialUIDelegate>//签友盟协议


@property(nonatomic,strong)UITableView *setTableView;

//准备俩个友盟的属性
@property(nonatomic,strong)NSString *shareImage;
@property(nonatomic,strong)NSString *urlStr;




@end

@implementation SetViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    //tableView
    self.setTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, WITDH, HEIGHT - 300) style:UITableViewStylePlain];
    self.setTableView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:self.setTableView];
    
    self.setTableView.delegate = self;
    self.setTableView.dataSource = self;
    
    self.setVCArray = [NSMutableArray arrayWithObjects:@"分享",@"收藏",nil];


    
    //注册
    [self.setTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"setCell"];
    
    
    
}

//一个友盟方法
-(void)share{
    UMSocialSnsPlatform *snsPlatform = [UMSocialSnsPlatformManager getSocialPlatformWithName:UMShareToSina];
    snsPlatform.displayName = @"新浪";
      //吧要分享的平台写到一个数组中
    snsPlatform.loginName = @"网易";
    
    UMSocialSnsPlatform *snsPlatform2 = [UMSocialSnsPlatformManager getSocialPlatformWithName:UMShareToTencent];
    snsPlatform2.displayName = @"腾讯微博";
    
    //分享第二步
    [UMSocialSnsService presentSnsIconSheetView:self
                                         appKey:@"558936d367e58eb81b001298"  shareText:@"你要分享的文字"
                                     shareImage:[UIImage imageNamed:@"Icon-29.png"]
                                shareToSnsNames:[NSArray arrayWithObjects:UMShareToSina,UMShareToTencent,UMShareToRenren,nil] delegate:self];
    
    
    
}




//cell的行数由collectArray中收藏的数量来决定
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    //return self.collectArray.count;
    return 2;
    
}

//cell的高度
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 50;
    
}

//cell的显示
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:setCell];
    
    cell.textLabel.text = [self.setVCArray objectAtIndex:indexPath.row];
    return cell;
    
}

//cell点击
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    if (indexPath.row == 0) {

        
            [self share];
    }else if (indexPath.row == 1) {
        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
        NSString *userName = [defaults objectForKey:@"userName"];
        NSMutableArray *array = [[DataBaseHandle shareDataBaseHandle]selectZhuanT:userName withCompare:NO];
        
//          //此处遇到问题,就是收藏的提示框显示不出来,后来发现是因为set上的view太小,并且我原来的想法是显示在详情页的正中间,但是setVC和detailVC是俩个完全没关系的界面,所以这时候要用通知,来传达我们想要的命令
//            //所以首先我们要在detailVC中建立一个通知的对象,现在看detailVC
//            NSLog(@"111111111**********");
            if ([array containsObject:self.model.title]) {//如果数组中包含这个题目
//                //则删除这个题目
                
            [[DataBaseHandle shareDataBaseHandle]ZhuanTDeleteTable:self.model.title];
//删除题目的情况下通知中心返回参数为0
                [[NSNotificationCenter defaultCenter]postNotificationName:@"changHUD" object:@"0"];
                
                NSLog(@"2222222*********");

            } else {
                NSLog(@"********这也是一个逻辑");
                //如果没有这个题目,就根据用户名插入表格
                [[DataBaseHandle shareDataBaseHandle]insertZhuanT:self.model withUserName:[[NSUserDefaults standardUserDefaults] objectForKey:@"lanou0306"]];
                //通知中心传过来的信息就是1
     [[NSNotificationCenter defaultCenter]postNotificationName:@"changHUD" object:@"1"];
            }
            NSLog(@"333333*******");
            NSLog(@"%@",array);
        
    }

    }
    
//}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
